﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.Supervise.Model;
using HZ.Supervise.IBLL;

public partial class TotalDrugs_HospitalCompanyDRugsTotal : BasePage
{
    public TotalDrugs_HospitalCompanyDRugsTotal()
    { }

    protected void Page_Load(object sender, EventArgs e)
    {
        QueryControl1.OnQueryClick += new EventHandler(QueryControl1_OnQueryClick);
        if (!IsPostBack)
        {
            ViewState["query"] = URLUtil.GetSuperviseQueryFromURL(Request);
            QueryControl1.BindDataPre((SuperviseQuery)ViewState["query"]);//绑定查询条件的一些数据源 质量层次等
            BindData();
            QueryControl1.BindSort(gvwInfo);
            GetHospitalCount();

        }
    }

    void QueryControl1_OnQueryClick(object sender, EventArgs e)
    {
        AspNetPager1.CurrentPageIndex = 1;
        ViewState["query"] = QueryControl1.UpdateQueryFromValue((SuperviseQuery)ViewState["query"]);
        BindData();
        GetHospitalCount();
    }


    private void BindData()
    {
        //int t = 0;
        //IList<HZ.Supervise.Model.View.Statistics_Company_Hospital> CompanyList;
        //HZ.Supervise.BLL.BLL_Statistics Bll_sta = (HZ.Supervise.BLL.BLL_Statistics)HZ.Supervise.BLLFactory.IBLL_Statistics();
        //HZ.Supervise.Model.Query.Statistics_Company_Hospital CompanyWhere = Sqlwhere();
        //CompanyList = Bll_sta.GetStatistics_Company_Hospital(CompanyWhere, AspNetPager1.CurrentPageIndex, AspNetPager1.PageCount, out t);

        //this.gvwInfo.DataSource = CompanyList;
        //this.gvwInfo.DataBind();

        int t = 0;
        HZ.Supervise.IBLL.IExecuteContext ie = HZ.Supervise.BLLFactory.ExecuteContext();
        ie.QueryObject = new HZ.Supervise.Model.QueryObject()
        {
            //分组字段
            GroupField = GroupByField.ProcureCatalogID | GroupByField.CompanyID_PS | GroupByField.HospitalID,
            //查询条件
            Query = (SuperviseQuery)ViewState["query"],
            //分组查询字段
            GroupSelectField =GroupFields.DistributeCountTotal | GroupFields.PurchaseCountTotal | GroupFields.WareHouseCountTotal | GroupFields.PurchaseAmountTotal,
            //排序字段
            SortField = QueryControl1.SortField == "" ? "ProductName ASC" : QueryControl1.SortField
        };

        //将数据填入gridview中
        System.Data.DataTable dt = ie.ExecutePage(AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out t);
        AspNetPager1.RecordCount = t;
        //显示相应的字段
        GridViewUtil.AdjustGridView(gvwInfo, dt);

        gvwInfo.DataSource = dt;
        gvwInfo.DataBind();
        SetColumnVisible();
    }


    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        BindData();
    }
    protected void gvwInfo_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "DirectByDetail")
        {
            //设置分组查询字段GroupSelectField
            if (ShowColumns.ShowColumn == null)
            {
                ShowColumns.ShowColumn = new ShowColumns();
                ShowColumns.ShowColumn.GroupFields = GroupFields.DistributeCountTotal | GroupFields.DistributeRate | GroupFields.PurchaseCountTotal | GroupFields.WareHouseCountTotal;
            }

            string sessionid = Guid.NewGuid().ToString();
            SuperviseQuery query = (SuperviseQuery)ViewState["query"];
            try
            {
                string[] estr = e.CommandArgument.ToString().Split(',');

                query.ProcureCatalogID = Convert.ToString(estr[0]);
                query.CompanyID_PS = Convert.ToString(estr[1]);
                query.HospitalID = Convert.ToString(estr[2]);

            }
            catch { }
            Session[sessionid] = query;
            string url = System.Configuration.ConfigurationManager.AppSettings["DetailInfo"];
            Response.Redirect(string.Format("{0}?sessionid={1}", url, sessionid));
        }
    }


    #region   数据准备

    /// <summary>
    /// 屏蔽相关查询连接
    /// </summary>
    private void SetColumnVisible()
    {
        //if (!string.IsNullOrEmpty(QueryControl1.ProcureCatalogID))
        //{
        //    foreach (GridViewRow row in gvwInfo.Rows)
        //    {
        //        row.FindControl("lbnDrug").Visible = false;
        //    }
        //}
        //if (!string.IsNullOrEmpty(QueryControl1.CompanyID_PS))
        //{
        //    foreach (GridViewRow row in gvwInfo.Rows)
        //    {
        //        row.FindControl("lbnCom_PS").Visible = false;
        //    }
        //}
        //if (!string.IsNullOrEmpty(QueryControl1.HospitalID))
        //{
        //    foreach (GridViewRow row in gvwInfo.Rows)
        //    {
        //        row.FindControl("lbnHos").Visible = false;
        //    }
        //}
    }
    #endregion


    protected void btnExcel_Click(object sender, EventArgs e)
    {
        int t = 0;
        HZ.Supervise.IBLL.IExecuteContext ie = HZ.Supervise.BLLFactory.ExecuteContext();
        ie.QueryObject = new HZ.Supervise.Model.QueryObject()
        {
            //分组字段
            GroupField = GroupByField.ProcureCatalogID | GroupByField.CompanyID_PS | GroupByField.HospitalID,
            //查询条件
            Query = (SuperviseQuery)ViewState["query"],
            //分组查询字段
            GroupSelectField = (ShowColumns.ShowColumn == null) ? GroupFields.DistributeCountTotal | GroupFields.PurchaseCountTotal | GroupFields.WareHouseCountTotal | GroupFields.PurchaseAmountTotal : ShowColumns.ShowColumn.GroupFields,
            //排序字段
            SortField = QueryControl1.SortField == "" ? "ProcureCatalogID ASC" : QueryControl1.SortField
        };

        //将数据填入gridview中
        System.Data.DataTable dt = ie.ExecutePage(0, 0, out t);
        //显示相应的字段
        //GridViewUtil.AdjustGridView(gvwInfo, dt);  
        ExportFile.Instance.ExportData(dt, gvwInfo, this.Page);
    }
    public void GetHospitalCount()
    {
        int t = 0;
        HZ.Supervise.IBLL.IExecuteContext ie = HZ.Supervise.BLLFactory.ExecuteContext();
        ie.QueryObject = new HZ.Supervise.Model.QueryObject()
        {
            //分组字段
            GroupField = GroupByField.ProcureCatalogID | GroupByField.CompanyID_PS | GroupByField.HospitalID,
            //查询条件
            Query = (SuperviseQuery)ViewState["query"],
            //分组查询字段
            GroupSelectField = GroupFields.DistributeCountTotal | GroupFields.PurchaseCountTotal | GroupFields.WareHouseCountTotal | GroupFields.PurchaseAmountTotal,
            //排序字段
            SortField = QueryControl1.SortField == "" ? "ProductName ASC" : QueryControl1.SortField
        };

 
        System.Data.DataTable dt = ie.ExecutePage(1,0, out t);
        double PurchaseAmountTotal = 0;//采购总金额
        int PurchaseCountTotal = 0;
        int  DistributeCountTotal = 0;
        int WareHouseCountTotal = 0;
        if (dt.Rows.Count > 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {

                PurchaseAmountTotal += HZ.MathUtil.SToF(dt.Rows[i]["PurchaseAmountTotal"].ToString());
                PurchaseCountTotal += HZ.MathUtil.SToI(dt.Rows[i]["PurchaseCountTotal"].ToString());
                DistributeCountTotal += HZ.MathUtil.SToI(dt.Rows[i]["DistributeCountTotal"].ToString());
                WareHouseCountTotal += HZ.MathUtil.SToI(dt.Rows[i]["WareHouseCountTotal"].ToString());
            }

            lblCount.Text = "采购总金额为:" + PurchaseAmountTotal.ToString("N2") + ";采购总数量为:" + PurchaseCountTotal.ToString() + ";配送总数量为:" + DistributeCountTotal.ToString() + ";入库总数量为:" + WareHouseCountTotal .ToString()+ "";
        }
        else
        {
            lblCount.Text = "暂无符合条件的数据！";
        }
    }
}
